//
//  Game.swift
//  Gamers
//
//  Created by 虚空之翼 on 15/7/17.
//  Copyright (c) 2015年 Freedom. All rights reserved.
//

import Foundation
import RealmSwift
import SwiftyJSON

class Game: Object {
    dynamic var id = ""             //ID
    dynamic var gameId = ""
    dynamic var imageSource = ""    //封面
    dynamic var platforms = ""
    dynamic var tags = ""
    dynamic var type = ""           //类型：1热门、2新游戏推荐

    var names = List<GameName>()
    var videos = List<Video>()
    
    dynamic var videoCount = 0      //视频总数
    
    // 计算本地游戏名称
    var localName: String {
        var chineseName = ""
        var englishName = ""
        
        for name in self.names {
            if name.language == "chinese" {
                chineseName = name.translation
            } else {
                englishName = name.translation
            }
        }

        let locallanguage = NSLocale.preferredLanguages()[0] //as! String
        if locallanguage.hasPrefix("zh-Hans") || locallanguage.hasPrefix("zh-Hant") || locallanguage.hasPrefix("zh-HK") {
            return chineseName
        } else {
            return englishName
        }

    }
    
    class func collection(json: JSON) -> [Game] {
        var collection = [Game]()
        
        if let items = json.array {
            for item in items {
                collection.append(Game.modelFromJSON(item))
            }
        }

        return collection
    }
    
    // 把JSON数据转换为对象
    class func modelFromJSON(json: JSON) -> Game {
        let model = Game()
        
        if let itemId = json["id"].string { model.id = itemId }
        if let gameId = json["game_id"].string { model.gameId = gameId }
        if let imageSource = json["image_source"].string { model.imageSource = imageSource }
        if let platforms = json["platforms"].string { model.platforms = platforms }
        if let tags = json["tags"].string { model.tags = tags }
        if let type = json["type"].string { model.type = type }
        if let videoCount = json["videoCount"].int { model.videoCount = videoCount }
        
        //model.videos = Video.collection(json["videos"])
        //model.names = GameName.collection(json["game_name"])
        let videos = Video.collection(json["videos"])
        for video in videos {
            model.videos.append(video)
        }
        
        let names = GameName.collection(json["game_name"])
        for name in names {
            model.names.append(name)
        }
        
        return model
    }
    
    class func collection2(json: JSON) -> [Video] {
        var collection = [Video]()
        
        if let items = json.array {
            for item in items {
                collection.append(Video.modelFromJSON(item))
            }
        }
        
        return collection
    }

}

class GameName: Object {
    dynamic var language = ""
    dynamic var translation = ""
    
    class func modelFromJSON(json: JSON) -> GameName {
        let model = GameName()
        if let language = json["language"].string { model.language = language }
        if let translation = json["translation"].string { model.translation = translation }
        
        return model
    }
    
    class func collection(json: JSON) -> [GameName] {
        var collection = [GameName]()
        
        if let items = json.array {
            for item in items {
                collection.append(GameName.modelFromJSON(item))
            }
        }
        
        return collection
    }

}